package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingPlanDto;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author Jiagege
 * @date 2025-07-20
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingProjectPlanServiceImpl nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = getById(id);

        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));

        List<NursingProjectPlanVo> projectPlanVos = projectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();

            nursingProjectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(String.valueOf(projectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setExecuteTime(projectPlan.getExecuteTime());
            nursingProjectPlanVo.setId(projectPlan.getId() );
            nursingProjectPlanVo.setPlanId(String.valueOf(Long.valueOf(projectPlan.getPlanId())));
            nursingProjectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {

        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class )
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        boolean flag = nursingProjectPlanMapper.insertBatch(nursingPlanDto.getProjectPlans(), nursingPlan.getId());
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlandto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class )
    public int updateNursingPlan(NursingPlanDto nursingPlandto)
    {
        //拷贝对象
//        Integer id = nursingPlandto.getId();
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlandto, nursingPlan);
        //获取护理项目计划集合
        List<NursingProjectPlan> projectPlans = nursingPlandto.getProjectPlans();
        //判断集合是否为空
        //若不为空，则遍历集合，将护理项目计划的计划ID设置为当前护理计划的ID 删除原有的护理项目计划，新增护理项目计划
        if(CollectionUtils.isNotEmpty(projectPlans)){
//            删除原有的护理项目计划
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        boolean flag = nursingPlanMapper.updateNursingPlan(nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        boolean flag = removeByIds(Arrays.asList(ids));
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag? 1 : 0;
    }



    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class )
    public int deleteNursingPlanByIds(Integer id) {

        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        boolean flag = removeById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag? 1 : 0;
    }

    @Override
    public List<NursingPlan> listAll() {
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get("nursingPlan:all");
        if(!CollectionUtils.isEmpty(list)){
            return list;
        }
        list = this.list();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
